import redis


class RedisCon():
    def __init__(self):
        self.conn = redis.Redis(host='localhost',port=6379,decode_responses=True)

    def list_push(self,key,value):
        self.conn.lpush(key,value)

    def list_pop(self,key):
        return self.conn.rpop(key)

    def list_len(self,key):
        if self.conn.exists():
            return self.conn.llen(key)
        else:
            return 0



    def insert_set(self,key,value):

        return self.conn.sadd(key,*value)


    def find_set(self,key):
        value = self.conn.smembers(key)
        if value:
            list = []
            for i in value:
                list.append(str(i,encoding='utf8'))
            return list
        else:
            return None
    # redis使用hash存储，查看，删除
    def insert_hash_set(self,key,params,value):
        self.conn.hset(key,params,value)
#    获取单个值
    def get_hash_params(self,key,params):
        return self.conn.hget(key,params)
#     获取全部
    def get_all_hash_params(self,key):
        return self.conn.hgetall(key)
#     删除指定
    def del_hash_params(self,key,params):
        self.conn.hdel(key,params)

#     字符串添加
    def set_str(self,key,value,time=0):
        if time>0:
            self.conn.setex(key,time,value)
        else:
            self.conn.set(key,value)
#     字符串获取
    def get_str(self,key):
        return self.conn.get(key)




r = RedisCon()
# value = ['/qwer','/sad']
# r.insert_set('qwer',value)
#
# res = r.find_set('qwer')
# print(res)
# r.insert_hash_set('abac','name1','12345')
# r.insert_hash_set('abac','time',"['2021-10-12','2021-12-12']")

# print(r.get_all_hash_params('abac'))